<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


    class Login extends CI_Controller
    {                              
        var $data;

        public function __construct()
        {
            parent::__construct();
            $this->initAdminData();
        }

        function index()
        {            
            $this->load->view('login_view',$this->data);
        }
        
        function verifyLogin()
        {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('username','Username','trim|required|xss_clean');
            $this->form_validation->set_rules('password','Password','trim|required|xss_clean|callback_check_database');
            
            if( $this->form_validation->run() == FALSE )
            {
                $this->load->view('login_view',$this->data);
            }
            else
            {
                redirect('admin/index','refresh');
            }
        }
        
        function logout()
        {
            $this->session->unset_userdata('logged_in');
            $this->load->view('login_view',$this->data);
        }
        
        function check_database($password)
        {                  
            $this->load->model('User_model');
            
            $username = $this->input->post('username');
            
            $result = $this->User_model->check($username,$password);

            if( $result )
            {
                $sess_array = array(
                    'id'    => $result[0]['id'],
                    'username'  => $result[0]['username'],
                );             
                
                $this->session->set_userdata('logged_in',$sess_array);
            }
            else
            {
                $this->form_validation->set_message('check_database','Invalid username or password');
                return false;
            }
        }

        function initAdminData()
        {
            $this->data = array(
                'title'   =>  'NDH Admin',                    
            );
        }
        
    }
    
?>
